package com.atguigu.vodservice.controller;


import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.handler.GuliException;
import com.atguigu.vodservice.utils.AliyunVodSDKUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Auther 赵乾坤
 * @Date 2020-11-27 23:16
 */
@Api(description="视频管理")
@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VodController {

    @ApiOperation(value = "上传视频文件")
    @PostMapping("uploadVideo")
    public R uploadVideo(MultipartFile file){
        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));
            UploadStreamRequest request = new UploadStreamRequest(
                    "LTAI4GKVG6ZjcLpmQpWoMFmg",
                    "EAsw6cjn7j4YcrshYZ6SGTdsyk51pC",
                    title,
                    originalFilename,
                    inputStream
            );
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

            //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。
            // 其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            String videoId = response.getVideoId();
            return R.ok().data("videoId", videoId);
        } catch (IOException e) {
            e.printStackTrace();
            throw new GuliException(20001, "上传失败");
        }

    }

    @ApiOperation(value = "删除视频文件")
    @DeleteMapping("deleteVideo/{videoId}")
    public R deleteVideo(@PathVariable String videoId) {
        try {
            //1.创建初始化对象
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient("LTAI4GKVG6ZjcLpmQpWoMFmg",
                    "EAsw6cjn7j4YcrshYZ6SGTdsyk51pC");
            //2.创建请求，响应对象
            DeleteVideoRequest  request = new DeleteVideoRequest();
            DeleteVideoResponse response = new DeleteVideoResponse();
            //3.向请求设置参数
                request.setVideoIds(videoId);
            //4.用初始化对象调用方法发送请求,拿到响应
                client.getAcsResponse(request);
            //5.从响应中获取参数
            return R.ok();
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001, "删除视频失败");
        }

    }




    @ApiOperation(value = "批量删除视频")
    @DeleteMapping("deleteVideoList")
    public R deleteVideoList(@RequestParam("videoIdList") List<String> videoIdList) {
        try {
            //1.创建初始化对象
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient("LTAI4GKVG6ZjcLpmQpWoMFmg",
                    "EAsw6cjn7j4YcrshYZ6SGTdsyk51pC");
            //2.创建请求，响应对象
            DeleteVideoRequest  request = new DeleteVideoRequest();
            DeleteVideoResponse response = new DeleteVideoResponse();
            //3.向请求设置参数
            //支持传入多个视频ID，多个用逗号分隔
            //3.1videoIdList转化成，分隔的字符串
            //11,12，13
//            String videoIds = StringUtils.join(videoIdList.toArray(), ",");
            String videoIds = StringUtils.join(videoIdList.toArray(),",");
//            request.setVideoIds(videoIds);
            //4.用初始化对象调用方法发送请求,拿到响应
            client.getAcsResponse(request);
            //5.从响应中获取参数
            return R.ok();
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001, "删除视频失败");
        }

    }

    @ApiOperation(value = "根据视频id获取视频播放凭证")
    @GetMapping("getPlayAuth/{vid}")
    public R getPlayAuth(@PathVariable String vid){
        try {
            //（1）创建初始化对象
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient("LTAI4GKVG6ZjcLpmQpWoMFmg", "EAsw6cjn7j4YcrshYZ6SGTdsyk51pC");
            //（2）创建request、response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
            //（3）向request设置视频id
            request.setVideoId(vid);
            //播放凭证有过期时间，默认值：100秒 。取值范围：100~3000。
            //request.setAuthInfoTimeout(200L);
            //（4）调用初始化方法实现功能
            response = client.getAcsResponse(request);
            //（5）调用方法返回response对象，获取内容
            String playAuth = response.getPlayAuth();
            return R.ok().data("playAuth",playAuth);
        } catch (ClientException e) {
            throw new GuliException(20001,"获取视频凭证失败");
        }
    }
}
